allow public access to socket and keep it open when requested#471
Open
Bersaelor wants to merge 1 commit intohttpswift:stablefrom
Open
allow public access to socket and keep it open when requested#471Bersaelor wants to merge 1 commit intohttpswift:stablefrom
Bersaelor wants to merge 1 commit intohttpswift:stablefrom
Conversation
Contributor
|
Hey @Bersaelor, could you give me an example of how you're using this? Also, could you fix the merge conflict while you're at it? |
Author
|
hey @michaelenger , from a UX perspective the user only opens the page in their browser and then never interacts with the desktop pc/mac anymore, instead going across the room and controlling the website with their phone. First I save the socket that is currently open when the website loads, like this: // during init
do {
try httpServer.start()
}
catch {
log.error("While starting httpserver: \(error)")
}
httpServer["/"] = htmlHandler(filePathComponent: "/Web/index.html", context: rootContext)
httpServer["/websocket-echo"] = websocket(
text: webSocket(session:text:),
binary: nil, pong: nil, connected: nil, disconnected: nil
)
}
fileprivate func webSocket(session: WebSocketSession, text: String) {
guard text.hasPrefix("BTI$") else {
// if text doesn't start with 'Browser To Iphone', then it's not a socket message
session.writeText(text)
return
}
log.debug("message: \(text)")
if self.currentSocket == nil {
self.socketDidOpen()
currentSocket = session.socket
session.socket.didClose = { [weak self] in
self?.socketDidClose()
self?.currentSocket = nil
}
}
guard let message = RemoteScreenMessage(message: text) else {
log.error("text couldn't be parsed: \( text )")
return
}
log.debug("Message: \(message)")
DispatchQueue.main.async {
self.received(message)
}
}and then when I want to make the currently presented desktop page change I do: fileprivate func sendToClient(_ message: MessageToBrowser) {
let msgString = "....."
log.debug("Attempting to send msg: \(msgString)")
networkingQueue.async {
guard let socket = self.currentSocket else { return }
let session = WebSocketSession(socket)
session.shouldCloseSocket = false
if message.type.shouldBeRepeated { self.lastMessage = message }
session.writeText(msgString)
}
} |
Contributor
|
This is a cool idea, what do you think @Vkt0r? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey there, I've been using swifter for a while, but in my case I was always keeping the sockets open, to allow for stateful connections.
In my case communication is bidirectional, the phone acts as a sort of remote for the browser window.
The below are all the changes I needed from this project, would this be something worth merging into the main branch?